package com.liukai.novel_rack.presenter.impl;

import com.liukai.novel_base.base.BasePresenter;
import com.liukai.novel_commons.API;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_commons.utils.RetrofitManager;
import com.liukai.novel_rack.call_back.IRackCallBack;
import com.liukai.novel_rack.call_back.ISearchCallBack;
import com.liukai.novel_rack.presenter.IRackPresenter;
import com.liukai.novel_rack.presenter.ISearchPresenter;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SearchPresenterImpl extends BasePresenter<ISearchCallBack> implements ISearchPresenter {


    /**
     * 根据小说名称模糊查询小说列表
     *
     * @param novelName
     */
    @Override
    public void findNovelLikeName(String novelName) {
        Retrofit retrofit = RetrofitManager.getNovelInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<Result> apiNovelLikeName = api.findNovelLikeName(novelName);
        apiNovelLikeName.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                LogUtils.e(getClass(), "response:" + response.toString());
                if (response.code() == HttpURLConnection.HTTP_OK) {
                    Result body = response.body();
                    LogUtils.e(getClass(), "body:" + body.toString());
                    mCallBack.findNovelLikeNameResult(response.body());

                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }


}
